package com.xyzh05.model;


import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

import org.compass.annotations.Searchable;
import org.compass.annotations.SearchableId;
import org.compass.annotations.SearchableProperty;
import org.hibernate.annotations.Formula;

@Entity
@Table(name="comment")
@Searchable
@XmlRootElement
public class Comment extends BaseObject implements Serializable {
	private static final long serialVersionUID = 859124866711371623L;
	
	private Long id;
    private Comment comment;
    private Date replyTime = new Date();
    private String content;
    private Short status;
    private Long ownerId;
    private Long ownerType;
    //private Long userId;
    private User user; 
    private String attachmentName;
    private Set<Comment> comments = new HashSet<Comment>(0);
    
    
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="user_id")
    public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Id @GeneratedValue(strategy=GenerationType.AUTO) @SearchableId    
    public Long getId() {
        return this.id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="parent_id")
    public Comment getComment() {
        return this.comment;
    }
    
    public void setComment(Comment comment) {
        this.comment = comment;
    }

    /**
    @Column(name="user_id", nullable=false)
    @SearchableProperty
    public Long getUserId() {
        return this.userId;
    }
    
    public void setUserId(Long userId) {
        this.userId = userId;
    }*/
    
    @Column(name="reply_time", nullable=false)
    @SearchableProperty
    public Date getReplyTime() {
        return this.replyTime;
    }
    
    public void setReplyTime(Date replyTime) {
        this.replyTime = replyTime;
    }
    
    @Column(name="content", nullable=false, length=1024)
    @SearchableProperty
    public String getContent() {
        return this.content;
    }
    
    public void setContent(String content) {
        this.content = content;
    }
    
    @Column(name="status", nullable=false)
    @SearchableProperty
    public Short getStatus() {
        return this.status;
    }
    
    public void setStatus(Short status) {
        this.status = status;
    }
    
    @Column(name="owner_id", nullable=false)
    @SearchableProperty
    public Long getOwnerId() {
        return this.ownerId;
    }
    
    public void setOwnerId(Long ownerId) {
        this.ownerId = ownerId;
    }
    
    @Column(name="owner_type", nullable=false)
    @SearchableProperty
    public Long getOwnerType() {
        return this.ownerType;
    }
    
    public void setOwnerType(Long ownerType) {
        this.ownerType = ownerType;
    }
    
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="comment")
    public Set<Comment> getComments() {
        return this.comments;
    }
    
    public void setComments(Set<Comment> comments) {
        this.comments = comments;
    }
    
    @Formula("(select a.name from attachment a where a.owner_id=id and a.owner_type = owner_type)")
	public String getAttachmentName() {
		return attachmentName;
	}

	public void setAttachmentName(String attachmentName) {
		this.attachmentName = attachmentName;
	}

	public Comment(){		
	}

	public Comment(Long id, Comment comment, Date replyTime, String content,
			Short status, Long ownerId, Long ownerType, Set<Comment> comments,
			User user) {
		this.id = id;
		this.comment = comment;
		this.replyTime = replyTime;
		this.content = content;
		this.status = status;
		this.ownerId = ownerId;
		this.ownerType = ownerType;
		this.comments = comments;
		this.user = user;
	}

	public Comment(Long id, Comment comment, Date replyTime, String content,
			Short status, Long ownerId, Long ownerType, Set<Comment> comments,
			User user, String attachmentName) {	
		this.id = id;
		this.comment = comment;
		this.replyTime = replyTime;
		this.content = content;
		this.status = status;
		this.ownerId = ownerId;
		this.ownerType = ownerType;
		this.comments = comments;
		this.user = user;
		this.attachmentName = attachmentName;
	}

	public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Comment pojo = (Comment) o;

        if (comment != null ? !comment.equals(pojo.comment) : pojo.comment != null) return false;
        //if (userId != null ? !userId.equals(pojo.userId) : pojo.userId != null) return false;
        if (user != null ? !user.equals(pojo.user) : pojo.user != null) return false;
        if (replyTime != null ? !replyTime.equals(pojo.replyTime) : pojo.replyTime != null) return false;
        if (content != null ? !content.equals(pojo.content) : pojo.content != null) return false;
        if (status != null ? !status.equals(pojo.status) : pojo.status != null) return false;
        if (ownerId != null ? !ownerId.equals(pojo.ownerId) : pojo.ownerId != null) return false;
        if (ownerType != null ? !ownerType.equals(pojo.ownerType) : pojo.ownerType != null) return false;

        return true;
    }

    public int hashCode() {
        int result = 0;
        result = (comment != null ? comment.hashCode() : 0);
        //result = 31 * result + (userId != null ? userId.hashCode() : 0);
        result = 31 * result + (user != null ? user.hashCode() : 0);
        result = 31 * result + (replyTime != null ? replyTime.hashCode() : 0);
        result = 31 * result + (content != null ? content.hashCode() : 0);
        result = 31 * result + (status != null ? status.hashCode() : 0);
        result = 31 * result + (ownerId != null ? ownerId.hashCode() : 0);
        result = 31 * result + (ownerType != null ? ownerType.hashCode() : 0);

        return result;
    }

    public String toString() {
        StringBuffer sb = new StringBuffer(getClass().getSimpleName());

        sb.append(" [");
        sb.append("id").append("='").append(getId()).append("', ");
        sb.append("comment").append("='").append(getComment()).append("', ");
        //sb.append("userId").append("='").append(getUserId()).append("', ");
        sb.append("user").append("='").append(getUser()).append("', ");
        sb.append("replyTime").append("='").append(getReplyTime()).append("', ");
        sb.append("content").append("='").append(getContent()).append("', ");
        sb.append("status").append("='").append(getStatus()).append("', ");
        sb.append("ownerId").append("='").append(getOwnerId()).append("', ");
        sb.append("ownerType").append("='").append(getOwnerType()).append("', ");
        sb.append("]");
      
        return sb.toString();
    }

}
